<?php

/* @project : Template
 * @auther  : 青云
 * @date    : 2018/1/10
 * @desc    : 用户模型
 */

namespace app\common\model;

use app\common\model\Dept as DeptModel;
use app\common\model\v1\Manager;
use Overtrue\Pinyin\Pinyin;

class Admin extends Base
{
    // 设置密码
    function setPasswordAttr($value, $data)
    {
        return hash_func($value);
    }

    // 设置搜字母
    function setLetterAttr($value, $data)
    {

    }

    // 获取列表
    function getAdminList($map = [], $append = [])
    {
        $where[] = ['is_del', 'eq', 0];
        return $this->where($where)->where($map)->append($append)->select()->toArray();
    }

    // 登录
    function login($account, $password)
    {
        // 查询用户信息
        $where[]    = array('account', 'eq', $account);
        $where[]    = array('password', 'eq', hash_func($password));
        $where[]    = array('is_del', 'eq', 0);
        $where[]    = array('is_valid', 'eq', 1);

        $field      = 'id,account,name,avatar,mobile';

        $admin_info = $this->where($where)->field($field)->find();
        return $admin_info;
    }


    public function getRoleNameAttr($value, $data)
    {
        $where_role_id[] = ['admin_id', 'eq', $data['id']];
        $role_id_list    = model('RoleAdmin')->where($where_role_id)->column('role_id');

        $where_role[]   = ['id', 'in', $role_id_list];
        $role_name_list = model('Role')->where($where_role)->column('name');

        return implode(',', $role_name_list);
    }

    public function getRoleIdAttr($value, $data)
    {
        $where_role_id[] = ['admin_id', 'eq', $data['id']];
        $role_id_list    = model('RoleAdmin')->where($where_role_id)->column('role_id');

        return $role_id_list;
    }

    public function getIsLeaderTextAttr($value, $data)
    {
        if ($data['is_leader'] == 1) {
            return '是';
        } else {
            return '';
        }
    }

    function getAdminListByDeptId($dept_id, $is_include_child = false)
    {
        $dept_model = new DeptModel();

        if ($is_include_child) {
            $child_dept_list = $dept_model->getChildDeptList($dept_id, $is_include_child);

            $dept_list   = $child_dept_list;
            $dept_list[] = $dept_id;

            $where_user[] = ['dept_id', 'in', $dept_list];

        } else {
            $where_user[] = ['dept_id', 'eq', $dept_id];
        }

        $field     = 'name,mobile,position_id';
        $user_list = $this->where($where_user)->field($field)->select();

        return $user_list;
    }

    static function isAdmin($admin_id)
    {
        $admin_list = config('config.admin_list');
        if (in_array($admin_id, $admin_list)) {
            return true;
        } else {
            return false;
        }
    }

    public function roleAdmin()
    {
        return $this->hasOne('app\common\model\RoleAdmin', 'admin_id', 'id');
    }
    public function getAccountTextAttr($value, $data)
    {
        $acc=Manager::where('admin_id',$data['id'])->value('manager_account');
        return $acc ?? '';
    }
    public function getPasswordTextAttr($value, $data)
    {
        $pass=Manager::where('admin_id',$data['id'])->value('manager_password');
        return $pass ?? '';
    }

}
